<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-9-06
 * Time: 下午4:31
 * To change this template use File | Settings | File Templates.
 */
class AdTypesModel extends Model{

    //根据cate_id 获取cate 信息
    public function get($ad_type_id){
        return $this->dbreader()->get($ad_type_id);
    }

    //获取ad_types 列表<带分页>
    public function getAdTypeList($curr_page,$per_page,$filter = array()){
        return $this->dbreader()->getAdTypeList($curr_page,$per_page,$filter);
    }

    //刷新分类树的预排序
    public function refreshAdTypeTree($right_value,$span = '+2'){
        return $this->dbreader()->refreshAdTypeTree($right_value,$span);
    }

    //由于树的顶部是虚拟的：left_value:0,right_value:max_value
    public function getMaxRightValue(){
        return $this->dbreader()->getMaxRightValue();
    }

    public function addAdType($type_name,$parent_ad_type_id){
        return $this->dbreader()->addAdType($type_name,$parent_ad_type_id);
    }

    //根据左右值获取子分类，包含本身
    public function getAdTypeByLR($left_value,$right_value){
        return $this->dbreader()->getAdTypeByLR($left_value,$right_value);
    }

    public function delAllAdType($ad_type_id){
        return $this->dbreader()->delAllAdType($ad_type_id);
    }

    public function updateAdType($ad_type_id,$type_name,$parent_ad_type_id = null){
        return $this->dbreader()->updateAdType($ad_type_id,$type_name,$parent_ad_type_id);
    }

    //获取用户拥有权限的菜单树
    public function getTree($ad_type_id = 0,$filter_child = false){
        return $this->dbreader()->getTree($ad_type_id, $filter_child);
    }

    //重组类型数组
    //$filter_child 是否过滤掉 子类 1 过滤 2 不过滤 3全显示 4 下拉框显示
    public function getArrayByAdTypeList($ad_type_id = 0,$filter_child = 2){
        $is_true = $filter_child == 1 ? true : false;
        $tree_arr = $this->getTree($ad_type_id, $is_true);

        $new_arr = array();

        foreach((array)$tree_arr as $k => $v){
            $new_arr[$v['ad_type_id']] = $v['type_name'];
        }

        $show_new_arr = array();

        foreach((array)$tree_arr as $k => $v){
            switch($filter_child){
                case 1:
                    if($v['parent_ad_type_id'] == 0){
                        $show_new_arr[$k][] = $v['ad_type_id'];
                        $show_new_arr[$k][]= $v['type_name'];
                    }
                    break;
                case 2:
                    if($v['parent_ad_type_id'] > 0){
                        $show_new_arr[$k][] = $v['ad_type_id'];
                        $show_new_arr[$k][] = $new_arr[$v['parent_ad_type_id']].'-'.$v['type_name'];
                    }
                    break;
                case 3:
                    $show_new_arr[$k][] = $v['ad_type_id'];
                    if($v['parent_ad_type_id'] > 0){
                        $show_new_arr[$k][] = $new_arr[$v['parent_ad_type_id']].'-'.$v['type_name'];
                    }else{
                        $show_new_arr[$k][] = $v['type_name'];
                    }
                    break;
                case 4:
                    if($v['parent_ad_type_id'] != 0){
                        $show_new_arr[$v['ad_type_id']] = $new_arr[$v['parent_ad_type_id']].'-'.$v['type_name'];
                    }
                    break;
            }
        }

        return $show_new_arr;
    }
}